<?php

use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Functions\Functions;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;

/**
 * @var Individual $individual
 * @var Family     $family
 * @var string     $title
 */

?>

<table class="table table-sm wt-facts-table wt-family-navigator-family">
    <caption class="text-center wt-family-navigator-family-heading">
        <a href="<?= e($family->url()) ?>">
            <?= $title ?>
        </a>
    </caption>
    <tbody>
        <?php foreach ($family->spouses() as $spouse) : ?>
            <tr class="text-center wt-family-navigator-parent wt-gender-<?= $spouse->sex() ?>">
                <th class="align-middle wt-family-navigator-label" scope="row">
                    <?php if ($spouse === $individual) : ?>
                        <?= Functions::getCloseRelationshipName($individual, $spouse) ?>
                        <i class="icon-selected"></i>
                    <?php elseif ($spouse->childFamilies()->isNotEmpty()) : ?>
                    <div class="dropdown">
                        <a class="dropdown-toggle" href="#" role="button" id="dropdown-<?= e($spouse->xref()) ?>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <?= Functions::getCloseRelationshipName($individual, $spouse) ?>
                        </a>

                        <div class="dropdown-menu wt-family-navigator-dropdown">
                            <?php foreach ($spouse->childFamilies() as $n => $spouse_child_family) : ?>
                                <?php if ($n > 0) : ?>
                                    <div class="dropdown-divider"></div>
                                <?php endif ?>
                                <a class="dropdown-header wt-family-navigator-dropdown-heading" href="<?= e($spouse_child_family->url()) ?>">
                                    <?= I18N::translate('Parents') ?>
                                </a>
                                <?php foreach ($spouse_child_family->spouses() as $parent) : ?>
                                    <a class="dropdown-item" href="<?= e($parent->url()) ?>">
                                        <?= $parent->fullName() ?>
                                    </a>
                                <?php endforeach ?>
                            <?php endforeach ?>
                        </div>
                    </div>
                    <?php else : ?>
                        <?= Functions::getCloseRelationshipName($individual, $spouse) ?>
                    <?php endif ?>
                </th>

                <td class="wt-family-navigator-name">
                    <?php if ($spouse->canShow()) : ?>
                        <a href="<?= e($spouse->url()) ?>">
                            <?= $spouse->fullName() ?>
                        </a>
                        <div class="small">
                            <?= $spouse->lifespan() ?>
                        </div>
                    <?php else : ?>
                        <?= $spouse->fullName() ?>
                    <?php endif ?>
                </td>
            </tr>
        <?php endforeach ?>

        <?php foreach ($family->children() as $child) : ?>
            <tr class="text-center wt-family-navigator-child wt-gender-<?= $child->sex() ?>">
                <th class="align-middle wt-family-navigator-label" scope="row">
                    <?php if ($child === $individual) : ?>
                        <?= Functions::getCloseRelationshipName($individual, $child) ?>
                        <i class="icon-selected"></i>
                    <?php elseif ($child->spouseFamilies()->isNotEmpty()) : ?>
                        <div class="dropdown">
                            <a class="dropdown-toggle" href="#" role="button" id="dropdown-<?= e($child->xref()) ?>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <?= Functions::getCloseRelationshipName($individual, $child) ?>
                            </a>

                            <div class="dropdown-menu">
                                <?php foreach ($child->spouseFamilies() as $n => $in_laws) : ?>
                                    <?php if ($n > 0) : ?>
                                        <div class="dropdown-divider"></div>
                                    <?php endif ?>
                                    <a class="dropdown-header wt-family-navigator-dropdown-heading" href="<?= e($in_laws->url()) ?>">
                                        <?= I18N::translate('Family') ?>
                                    </a>

                                    <?php foreach ($in_laws->spouses() as $sibling_in_law) : ?>
                                        <?php if ($sibling_in_law !== $child) : ?>
                                            <a class="dropdown-item" href="<?= e($sibling_in_law->url()) ?>">
                                                <?= $sibling_in_law->fullName() ?>
                                            </a>
                                        <?php endif ?>
                                    <?php endforeach ?>

                                <ul>
                                    <?php foreach ($in_laws->children() as $child_in_law) : ?>
                                    <li>
                                        <a class="dropdown-item" href="<?= e($child_in_law->url()) ?>">
                                            <?= $child_in_law->fullName() ?>
                                        </a>
                                    </li>
                                    <?php endforeach ?>
                                </ul>
                                <?php endforeach ?>
                            </div>
                        </div>
                    <?php else : ?>
                        <?= Functions::getCloseRelationshipName($individual, $child) ?>
                    <?php endif ?>
                </th>

                <td>
                    <?php if ($child->canShow()) : ?>
                        <a  href="<?= e($child->url()) ?>">
                            <?= $child->fullName() ?>
                        </a>
                        <div class="small">
                            <?= $child->lifespan() ?>
                        </div>
                    <?php else : ?>
                        <?= $child->fullName() ?>
                    <?php endif ?>
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>
